/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.system.service.impl;

import me.zhengjie.modules.system.domain.UserChat;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import me.zhengjie.modules.system.repository.UserChatRepository;
import me.zhengjie.modules.system.service.UserChatService;
import me.zhengjie.modules.system.service.dto.UserChatDto;
import me.zhengjie.modules.system.service.dto.UserChatQueryCriteria;
import me.zhengjie.modules.system.service.mapstruct.UserChatMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @website https://el-admin.vip
* @description 服务实现
* @author 王宇
* @date 2024-04-06
**/
@Service
@RequiredArgsConstructor
public class UserChatServiceImpl implements UserChatService {

    private final UserChatRepository userChatRepository;
    private final UserChatMapper userChatMapper;

    @Override
    public Map<String,Object> queryAll(UserChatQueryCriteria criteria, Pageable pageable){
        Page<UserChat> page = userChatRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(userChatMapper::toDto));
    }

    @Override
    public List<UserChatDto> queryAll(UserChatQueryCriteria criteria){
        return userChatMapper.toDto(userChatRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public UserChatDto findById(Long id) {
        UserChat userChat = userChatRepository.findById(id).orElseGet(UserChat::new);
        ValidationUtil.isNull(userChat.getId(),"UserChat","id",id);
        return userChatMapper.toDto(userChat);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public UserChatDto create(UserChat resources) {
        return userChatMapper.toDto(userChatRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(UserChat resources) {
        UserChat userChat = userChatRepository.findById(resources.getId()).orElseGet(UserChat::new);
        ValidationUtil.isNull( userChat.getId(),"UserChat","id",resources.getId());
        userChat.copy(resources);
        userChatRepository.save(userChat);
    }

    @Override
    public void deleteAll(Long[] ids) {
        for (Long id : ids) {
            userChatRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<UserChatDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (UserChatDto userChat : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("问题类型，0：文字", userChat.getQuestionType());
            map.put("问题", userChat.getQuestion());
            map.put("答案", userChat.getAnswer());
            map.put("用户openId", userChat.getOpenId());
            map.put("创建时间", userChat.getCreateTime());
            map.put("状态", userChat.getChatStatus());
            map.put("渠道", userChat.getChatChannel());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}